from apps.house.models import House
from apps.orders.models import Order


def all_house_detil(all_house):
    detail = {
        'acreage': all_house.acreage,
        'title': all_house.title,
        'price': all_house.price,
        'area_id': all_house.area_id,
        'address': all_house.address,
        'room_count': all_house.room_count,
        'unit': all_house.unit,
        'capacity': all_house.capacity,
        'beds': all_house.beds,
        'deposit': all_house.deposit,
        'min_days': all_house.min_days,
        'max_days': all_house.max_days,
        'facilities': all_house.facilities,
        'hid': all_house.id,
    }
    '''
    下面是用来补充上面 字典的 k键的，需要的是图片url和设施信息id列表
    '''
    # 获取图片
    img_urls = []
    # 一找多 一个房子 找多张图片 用模型类小写
    imgs_urls = all_house.houseimage_set.all()
    # 获取到对应的图片 获取url
    for img in imgs_urls:
        img_urls.append(img.url)

        # 给字典添加key 和v
    detail['img_urls'] = img_urls

    # 设施信息id列表
    facilities = []  # 创建列表
    facility = all_house.facility_set.all()  # 获取信息
    for i in facilities:  # 循环遍历
        facility.append(i)  # 添加进去
        # 给字典添加key 和v
    detail['facilities'] = facility  # 存入字典

    # 获取评论 把评论添加进表格。并且添加到字典detail里面
    comments = []
    # 要找到评论表里面，该房子对应的评论 连接Orders表
    comment = Order.objects.filter(house=all_house.id, status='COMPLETE')
    for com in comment:
        com = {
            "comment": com.comment,  # 评论的内容
            "user_name": com.user.username if com.user.username != com.user.mobile else "匿名用户",  # 发表评论的用户
            "ctime": com.update_time.strftime("%Y-%m-%d %H:%M:%S")  # 评价的时间
        }
        comments.append(com)
    detail['comments'] = comments
    return  detail
    # return  all_house_detil
    pass